<?php
function headerHTML($opt=array()){
	$winTit = $tit2 = '';
	if (is_string($opt)) { $winTit=$opt; }
	else {
		if (is_array($opt)) {
			if (isset($opt['winTit'])) $winTit = $opt['winTit'];
			if (isset($opt['tit2'])) $tit2 = $opt['tit2'];
		}
	}
	if (defined('HEADER_TEXT')) {
		$winTit = HEADER_TEXT.' - '.$tit2;
		$tit1 = HEADER_TEXT;
	}
	if ($winTit == '') $winTit = "POLARIS - MB-Admin - ".$tit2;
	$sHTML = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\n";
	$sHTML .= "<html>\n";
	$sHTML .= "<head>\n";
	$sHTML .= "<meta name=\"expires\" content= \"Mon, 1 Jan 2000 00:00:00 GMT\">\n";
	$sHTML .= "<meta http-equiv=\"Pragma\" content=\"no-cache\">\n";
	$sHTML .= "<meta http-equiv=\"Cache-Control\" content=\"no-cache\">\n";
	$sHTML .= "<meta http-equiv=\"content-language\" content=\"it\">\n";
	$sHTML .= "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\n";
	$sHTML .= "<title>$winTit</title>\n";
	$sHTML .= "<link rel=\"shortcut icon\" href=\"".SERVER_URL_ROOT."/app/_img/icoPolaris.ico\" type=\"image/x-icon\">\n";
	$sHTML .= "<link rel=\"stylesheet\" href=\"".SERVER_URL_ROOT."/app/_com/stileMB.css.php\" type=\"text/css\" />\n";
	$sHTML .= "<script language=\"JavaScript\" type=\"text/javascript\">\n";
	$sHTML .= "   var doubleClickPrevent = true;\n";
	$sHTML .= "   function pageRedir(url) {\n";			
	$sHTML .= "      if (doubleClickPrevent) {\n";
	$sHTML .= "         doubleClickPrevent = false;\n";
	$sHTML .= "         location.href=url;\n";
	$sHTML .= "      }\n";
	$sHTML .= "   }\n";
	$sHTML .= "</script>\n";
	$sHTML .= "</head>\n";
	return $sHTML;
}

function bodyHTML($opt=array()){
	global $objSDAO, $aMetaConPar, $isReloadable, $errMsg, $objUser;
	$aMenu = array();
	require 'inc.aMenu.php';
	$tit1 = $tit2 = $kMenu = '';
	if ($isReloadable !== false) $isReloadable = true;
	if (is_array($opt)) {
		$kMenu = sxVx($opt,'kMenu');
		$aX = explode('.',$kMenu);
		$kMenu1 = $aX[0];
		$kMenu2 = (isset($aX[1]))?$aX[1]:'';
		foreach($aMenu as $nM => $aLM) {
			if ($aLM['kMenu']==$kMenu1) {
				$tit2 = $aLM['text'];
			}
		}
		foreach($aMenu as $nM => $aLM) {
			if ($aLM['kMenu']==$kMenu) {
				$tit2 .= " - ".$aLM['text'];
			}
		}
		if (isset($opt['tit1'])) $tit1 = $opt['tit1'];
		if (isset($opt['tit2'])) $tit2 = $opt['tit2'];
		if (isset($opt['isReloadable'])) $isReloadable = $opt['isReloadable'];
	}
	if (defined('HEADER_TEXT')) {
		$tit1 = HEADER_TEXT;
	}
	if ($tit1 == '') $tit1 = "POLARIS - Meta-Base Manager";
	if ($tit2 == '') $tit2 = $_SERVER['SCRIPT_NAME'];
	$sHTML = '';
	if (!is_object($objSDAO)) $objSDAO = new SDAO($aMetaConPar);
	if(isset($_SESSION[USEROBJ])){ $objUte = $_SESSION[USEROBJ]; }
	$sSQL = "SELECT count(*) AS num FROM session " .
			"WHERE log_out = 'N' " .
				"AND time_last > TIMESTAMP('".date('Y-m-d H:i:s',time()-900)."') " ;	
	$objSDAO->query($sSQL,"cntSess",array("contOnErr"=>true));
	$numUtConn = '?';
	if ($row =$objSDAO->fetch("cntSess")) { $numUtConn = $row['num']; }
	$sBackGround = "";//background-image:url('".SERVER_URL_ROOT."/app/_img/bckgrnd01.jpg');";
	$sHTML .= "<BODY style=\"margin:0;$sBackGround\">\n";
	$sHTML .= "<div style='margin:auto;width:1100px'>\n";
	$sHTML .= "<table cellpadding=\"3\" cellspacing=\"5\" width=\"100%\" border='0'>\n";
	//first row - 2 cells
	$sHTML .= "<tr>";
	$sHTML .= "<td class=\"header1\" width='200' style='background-color:#ffff00;padding:10px;' align='center'>";
	$sHTML .= "<img src=\"".SERVER_URL_ROOT."/app/_img/logoPolaris.png\" border='0'>";
	$sHTML .= "</td>";
	$sHTML .= "<td class=\"header1\">";
	$sHTML .= "<table width='100%' border='0'><tr><td class=\"header1\">";
	if ($isReloadable)
		$sHTML .= "<a ".goToPag(SERVER_URL_ROOT."/app/_com/menu.php?xsn=".XSN)." class=\"header1\">";
	$sHTML .= $tit1 ;
	if ($isReloadable)
		$sHTML .= "</a>";	
	$sHTML .= "</td><td class=\"header9\">";
	$sHTML .= $objSDAO->dbcPar["dbName"]."@".$objSDAO->dbcPar["host"]." (".$objSDAO->dbcPar["dbcType"].
		") - users on: $numUtConn<br>";
	if(isset($objUte)){
		if($objUte->isAdmin){
			$sHTML .= ucfirst($objUte->name)." ".ucfirst($objUte->surname)." [admin mode]";
		}else{
			$sHTML .= ucfirst($objUte->name)." ".ucfirst($objUte->surname);
		}
	}
	$sHTML .= "</td>";
	$sHTML .= "</tr>\n";

	$sHTML .= "<tr><td class=\"header2\" style='text-align:left;'>$tit2</td>\n";
	$sHTML .= "<td class=\"header9\">&nbsp;\n";
	if ($isReloadable) {
		$sHTML .= "<a href='#' onClick=\"window.open('"
			.SERVER_URL_ROOT."/app/_com/cloneSession.php?xsn=".XSN."','_blank');\"" 
			." class=\"link01\" title=\""._t("Open a new window and a new session")."\"><b>new window</b></a>\n";	
		$sHTML .= "&nbsp;&nbsp;\n";
		$sHTML .= "<a ".goToPag(SERVER_URL_ROOT."/app/_com/logout.php?xsn=".XSN)
			." class=\"link01\" title=\""._t("close session and back to user/password form (log-in)")."\">"
			."<b>log-out</b></a>\n";	
		$sHTML .= "&nbsp;&nbsp;\n";
		$sHTML .= "<a href='".SERVER_URL_ROOT."/app/_com/polaris_project_description.pdf' target='_blank' "
			." class=\"link01\" title=\"Guide to meta-base management\">"
			."<b>guide</b></a>\n";	
	}
	$sHTML .= "</td>\n";
	$sHTML .= "</tr></table></td>\n";
	$sHTML .= "</tr>\n";

	$sHTML .= "<tr>\n";
	$sHTML .= "<td valign='top' style='padding:0;font-size:5px;'>\n"; //menu cell
	$sEndTd = '';
	foreach($aMenu as $nM => $aLM) {
		$aX = explode('.',$aLM['kMenu']);
		$kM1 = $aX[0];
		$kM2 = (isset($aX[1]))?$aX[1]:'';
		$bHavePerm = false;
		if(isset($aLM["text"])){
			$cmdPerm = (isset($aLM["perm"]))?$aLM["perm"]:"";
			//check data area permission 
			if($cmdPerm == ""){
				$bHavePerm = true;
			}
			//check user permission 
			if($objUser->getPermission($cmdPerm) == "Y"){
				$bHavePerm = true;
			}
		}
		if($bHavePerm){	
			if(isset($aLM["pag"])){
				$zz = (strpos($aLM["pag"],'?')===false)?'?':'&';
				if ($kM2=='') {
					if ($kM1 == $kMenu1) {
						$sStyle='color:red;';
					} else {
						$sStyle='';
					}
					$sHTML .= $sEndTd;
					$sHTML .= "<div class='menu03' align='left' valign='top'>\n";
					$sHTML .= "<div style='padding-top:5px;font-weight:bold;'>$nM.&nbsp;";
					$sHTML .= "<a class=\"menu03\" href=\"javascript:pageRedir('".$aLM["pag"].$zz."&hstry=res&xsn=".XSN."');\" ";
					if(isset($aLM["title"]))
						$sHTML .= "alt=\"".$aLM["title"]."\" title=\"".$aLM["title"]."\"";
					$sHTML .= " style='$sStyle'>".$aLM["text"]."</a></div>\n";
					$sEndTd = "<br/></div>\n<br>\n";
				} else {
					if ($aLM['kMenu'] == $kMenu) {
						$sStyle='color:red;';
					} else {
						$sStyle='';
					}
					$sHTML .= "<div style='padding-top:5px;'>&nbsp;&nbsp;&nbsp;&nbsp;$nM&nbsp;"
						."<a class=\"menu03\" href=\"javascript:pageRedir('".$aLM["pag"].$zz."&hstry=res&xsn=".XSN."');\" ";
					if(isset($aLM["title"]))
						$sHTML .= "alt=\"".$aLM["title"]."\" title=\"".$aLM["title"]."\"";
					$sHTML .= " style='$sStyle'>".$aLM["text"]."</a></div>\n";
				}
			}
		} 		
	}	
	$sHTML .= "<br/></div><br/>\n";
	$sHTML .= "<div class='menu03' align='left' valign='top'>\n";
	$sHTML .= "<br/><br/><br/><br/><br/><br/><br/><br/>";
	$sHTML .= "<br/></div>\n</td>\n";
		
	$sHTML .= "<td valign='top' colspan='2' class='tabl01'>\n";//content
	if (!isset($errMsg)) $errMsg = '';
	$errMsg .= sxREQ('msg');
	$sHTML .= "<div id=\"messArea\" class=\"mess01\">$errMsg</div>";
	return $sHTML;
}

function bodyPopUpHTML($opt=array()){
	$tit1 = $tit2 = '';
	if (is_array($opt)) {
		if (isset($opt['tit1'])) $tit1 = $opt['tit1'];
		if (isset($opt['tit2'])) $tit2 = $opt['tit2'];
	}
	if (defined('HEADER_TEXT')) {
		$tit1 = HEADER_TEXT;
	}
	if ($tit1 == '') $tit1 = "POLARIS - Meta-Base Administrator";
	if ($tit2 == '') $tit2 = $_SERVER['SCRIPT_NAME'];
	$sHTML = '';
	$sHTML .= "<body style='margin:5 10 10 10' onload='window.focus();'>\n";
	$sHTML .= "<table cellpadding=\"2\" cellspacing=\"0\" width=\"100%\" border='0'>\n";
	$sHTML .= "<tr>";
	$sHTML .= "   <td class=\"header3\" rowspan='2' width='30'>";
	$sHTML .= "      <img src=\"".SERVER_URL_ROOT."/app/_img/icoMB.gif\" width='30' border='0' class=\"header1\">";
	$sHTML .= "   </td>";
	$sHTML .= "   <td class=\"header3\" colspan='2'>$tit1</td>";
	$sHTML .= "</tr>\n";
	$sHTML .= "<tr>\n";
	$sHTML .= "   <td class=\"header4\" style='text-align:left;'>$tit2</td>\n";
	$sHTML .= "   <td class=\"header4\" style='text-align:right;'>\n";
	$sHTML .= "<span onClick=\"JavaScript:window.close();\" class=\"header9\" title=\"".
		_t("Chiude la finestra")."\" style='text-decoration:underline;font-weight:bold;cursor:pointer;'>".
		_t('chiudi')."</span>\n";	
	$sHTML .= "</td>\n";
	$sHTML .= "</tr>\n";
	$sHTML .= "<tr><td colspan='3' class=\"header5\">";
	//$sHTML .= "<hr class='header5' style='border-style:solid none none none; border-width:2px;'>";
	$sHTML .= "</td></tr>\n";
	$sHTML .= "</table>\n";	
	return $sHTML;
}

function footerHTML(){
	global $fh_dbg_, $objSDAO;
	$sHTML = "";
	$sHTML .= "</td>\n</tr>\n</table>\n";
	$sHTML .= "</div>\n<div style='margin:10;border-top:1px solid black;'>\n";
	$sHTML .= "DEBUG INFO - page: ".$_SERVER['PHP_SELF']." - sess.:".session_id()." - sub-sess.:".XSN." - ";
	if (file_exists(SERVER_FS_ROOT."/version")) {
		$sHTML .= 'version: '.file_get_contents(SERVER_FS_ROOT."/version");
	} else {
		$sHTML .= $_SERVER['HTTP_HOST']." (no 'version' file)";
	}
	$sHTML .= "<br/>\n";
	if(defined('USEROBJ') and is_object($_SESSION[USEROBJ])){
		$sHTML .=  "<pre width='132'>\r\n";
	   if ($_SESSION[USEROBJ]->debug['display']) {
	      if ($fh_dbg_) {
	         @fclose($fh_dbg_);
	      }
	      $fileLog = SERVER_FS_ROOT."/tmp/".session_id()."_".SUB_SESSION_NUMBER.".txt";
	      if (file_exists($fileLog) and $fh_dbg_ = @fopen($fileLog,"rb")) {
	      	$sHTML .=  "--------   DEBUG MESSAGE: \r\n";         
	         while($riga = @fgets($fh_dbg_,4096)) {
	            $sHTML .= $riga;
	         }
	         @fclose($fh_dbg_);
	         unset($fh_dbg_);
	         @unlink($fileLog);
	      } 
	      $sHTML .= "\r\n";
	      if ($_SESSION[USEROBJ]->debug['get']) {
	         if (empty($_GET)) {
	            $sHTML .=  "<b>GET</b> (vuoto)\r\n";
	         } else {
	            $sHTML .=  "<b>GET</b> ";
	            $sHTML .=  print_r($_GET,1);
	         }	
	      	$sHTML .= "\r\n";
	      }
	      if ($_SESSION[USEROBJ]->debug['post']) {
	         if (empty($_POST)) {
	            $sHTML .=  "<b>POST</b> (vuoto)\r\n";
	         } else {
	            $sHTML .=  "<b>POST</b>\n";
	            $sHTML .=  print_r($_POST,1);
	         }
	      	$sHTML .= "\r\n";
	      }
	      if ($_SESSION[USEROBJ]->debug['user'] and !$_SESSION[USEROBJ]->debug['session']) {
	         $sHTML .=  "<b>OGGETTO UTENTE</b>\n";
	         $sHTML .=  print_r($_SESSION[USEROBJ],1);
	      	$sHTML .= "\r\n";
	      }
	      if ($_SESSION[USEROBJ]->debug['session']) {
	         $sHTML .=  "<b>SESSIONE</b>\n ";
	         $sHTML .=  print_r($_SESSION,1);
	      	$sHTML .= "\r\n";
	      }
			// view error_log
	      if ($_SESSION[USEROBJ]->debug['errorLog'] > 0) {
	         $sHTML .=  "<b>ERROR_LOG</b>\r\n";
				$sSQL = "SELECT el.*, u.uid FROM error_log el " .
						"LEFT JOIN user u ON u.id_user = el.id_user " .
						"ORDER BY el.date_time_in DESC" ;	
				if ($objSDAO->query($sSQL,"viewErLog",array("contOnErr"=>true))) {
					$cnt = $_SESSION[USEROBJ]->debug['errorLog'];
					while($row =$objSDAO->fetch("viewErLog")) { 
						$sHTML .= $row['date_time_in']." - ".
							$row['uid']." - ".
							$row['error_code']." - ".
							$row['error_text']."\r\n";
						if ($cnt-- <= 1) break;
					}
	      	} else {
	      		$sHTML .= "$sSQL\r\n";
	      	}
	      	$sHTML .= "\r\n";
	      }
			// view oper_log
	      if ($_SESSION[USEROBJ]->debug['operLog'] > 0) {
	         $sHTML .=  "<b>OPER_LOG</b>\r\n";
				$sSQL = "SELECT ol.*, u.uid FROM oper_log ol " .
						"LEFT JOIN user u ON u.id_user = ol.id_user " .
						"ORDER BY ol.date_time_in DESC" ;	
				if ($objSDAO->query($sSQL,"viewOpLog",array("contOnErr"=>true))) {
					$cnt = $_SESSION[USEROBJ]->debug['operLog'];
					while($row =$objSDAO->fetch("viewOpLog")) { 
						$sHTML .= $row['date_time_in']." - ".
							$row['uid']." - ".
							$row['oper_code']." - ".
							$row['oper_text']."\r\n";
						if ($cnt-- <= 1) break;
					}
	      	} else {
	      		$sHTML .= "$sSQL\r\n";
	      	}
	      	$sHTML .= "\r\n";
	      }
	      $sHTML .=  "</pre>\r\n";
	   }
	}

	$sHTML .= "</div>\n";
	$sHTML .= "<script>\n";
	global $mcrTmStrt;
	$scnds = number_format(microtime(true)-$mcrTmStrt,3,',','.');
	$sHTML .= "   window.status = '$scnds sec.';";
	$sHTML .= "</script>\n";	
	$sHTML .= "</body>\n</html>\n";
	
	$objSDAO->close();
	return $sHTML;
}
